package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	import flash.utils.setInterval;
	
	public class Game extends MovieClip
	{
		var retryButton:theButton=new theButton();
		var mainScoreBox:TextField = new TextField();
		var score:int;
		var mainTank:Tank = new Tank();
		var mainBird:bird = new bird();
		var birdSpeed=mainBird.vx;
		var poopSpeed=mainBird.myPoop.vy;
		var bulletSpeed=mainTank.myBullet.vy;
	
		public function Game()
		{
			super();
			
			retryButton.x=434.5;
			retryButton.y=372.45;
			retryButton.displayText.text="retry";
			mainScoreBox.x=55;
			mainScoreBox.y=15.25;
			score = 0;
			stage.focus=stage;
			mainTank.x=250;
			mainTank.y=stage.stageHeight-100;
			mainBird.x=stage.stageWidth;
			mainBird.y=100;
			addChild(mainScoreBox);
			addChild(mainBird);
			addChild(mainTank);
			addChild(retryButton);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyboardDown_Handler );
			stage.addEventListener(KeyboardEvent.KEY_UP,keyboardUp_Handler );
			retryButton.addEventListener(MouseEvent.CLICK, clickHandler);
			
		
			this.addEventListener(Event.ENTER_FRAME,update);
		}
		
		public function clickHandler(e:MouseEvent):void
		{
			mainTank.x=250;
			mainTank.y=stage.stageHeight-100;
			mainBird.x=stage.stageWidth;
			mainBird.y=100;
			mainTank.vx=0;
			mainTank.myBullet.vy=bulletSpeed;
			mainBird.myPoop.vy=poopSpeed;
			mainBird.vx = birdSpeed;
			score=0;
			mainScoreBox.text=score.toString();
			//mainBird = new bird();
			if(mainTank.stage)
			{}
			else
			{
				addChild(mainTank);
			}
		}
		
		public function keyboardDown_Handler(e:KeyboardEvent):void
		{
			switch (e.keyCode) {
				case Keyboard.LEFT:
					//left boundary
					if(mainTank.stage && mainTank.x > (0+mainTank.width/2))
					{
						mainTank.vx=-8;
					}
					if(mainTank.stage && mainTank.x <= (0+mainTank.width/2))
					{
						mainTank.vx=0;
					}
					break;
				case Keyboard.SPACE:
					if(mainTank.stage)
					{
						mainTank.fire();
					mainTank.vx=mainTank.vx;
					}
					break;
				case Keyboard.RIGHT:
					if(mainTank.stage && mainTank.x < stage.stageWidth-mainTank.width/2)
					{
						mainTank.vx=8;
					}
					if(mainTank.stage && mainTank.x >= stage.stageWidth-mainTank.width/2)
					{
						mainTank.vx=0;
					}
					break;
				
			}
		}
		
		public function update(e:Event):void
		{
				if(mainBird.hitTestPoint(mainTank.myBullet.x,mainTank.myBullet.y) && mainBird.stage)
				{
					mainBird.removeEventListener(Event.ENTER_FRAME, mainBird.updater);
					//mainTank.myBullet.removeEventListener(Event.ENTER_FRAME, mainTank.myBullet.move);
					//if (mainBird.stage){
						mainBird.mySquawk.play();
						score+=1;
						mainScoreBox.text=score.toString();
						removeChild(mainBird);
					//}
					if(mainTank.myBullet.stage)
					{
						stage.removeChild(mainTank.myBullet);
						mainBird.x=stage.stageWidth;
						mainBird.y=100;
						mainBird.shitSpeed-=2;
						mainBird.vx+=5;
					}
					
				//	setInterval(updateBird, 3000);
					mainTank.myBullet.vy+=1;
					mainBird.myPoop.vy-=2;
					addChild(mainBird);
					mainBird.addEventListener(Event.ENTER_FRAME, mainBird.updater);
				}
			if(mainTank.hitTestPoint(mainBird.myPoop.x,mainBird.myPoop.y))
			{
				if (mainTank.stage){
					mainTank.removeEventListener(Event.ENTER_FRAME,mainTank.update);
					removeChild(mainTank);
					stage.removeChild(mainBird.myPoop);
					
				}
			}
			
		}
		
		public function keyboardUp_Handler(e:KeyboardEvent):void
		{
			if (e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.RIGHT)
			{
				mainTank.vx = 0;
				
			}
		}
		
	}
}